package TAIC.Classifier;

public class Pearson extends Divergence {

	public static void main ( String str [] ) {
		double [] p = new double [ 4 ] ;
		double [] q = new double [ 4 ] ;
		p [ 1 ] = 2 ; p [ 2 ] = 3 ; p [ 3 ] = 5 ; 
		q [ 1 ] = 22 ; q [ 2 ] = 2 ; q [ 3 ] = 13 ; 
		System.out.println ( (new Pearson()).cale ( p , q ));
	}
	
	
	@Override
	public double cale(double[] x, double[] y) {
		int N = Math.min( x.length, y.length ) ;
		double sum_sq_x = 0 ;
		double sum_sq_y = 0;
		double sum_coproduct = 0 ;
		double mean_x = x[0] ;
		double mean_y = y[0] ;
		for ( int i = 1 ; i < N ; i ++ ) {
		    double sweep = (i - 1.0) / i ;
		    double delta_x = x[i] - mean_x ;
		    double delta_y = y[i] - mean_y ;
		    sum_sq_x += delta_x * delta_x * sweep ;
		    sum_sq_y += delta_y * delta_y * sweep ;
		    sum_coproduct += delta_x * delta_y * sweep ;
		    mean_x += delta_x / i ;
		    mean_y += delta_y / i  ;
		}
		double pop_sd_x = Math.sqrt( sum_sq_x / N ) ;
		double pop_sd_y = Math.sqrt( sum_sq_y / N ) ;
		double cov_x_y = sum_coproduct / N ;
		double correlation = cov_x_y / (pop_sd_x * pop_sd_y) ;

		return -correlation;
	}
	
	public void normalize ( double p [] ) {
		int VECSIZE = p.length ; 
		double total = 0.0 ; 
		double eps = 0.001 ; 
		for ( int i = 0; i < VECSIZE ; i ++) total += p [ i ] ;
		for ( int i = 0 ; i < VECSIZE ; i ++ ) p [ i ] = ( 1 +p [ i ]) / (total +  VECSIZE ); 
		//for ( int i = 0 ; i < VECSIZE ; i ++ ) p [ i ] = ( 1- eps ) * p [ i ] / total + eps / VECSIZE ; 
	}

}
